package com.guat.dao;

import com.guat.entity.Article;
import com.guat.entity.User;
import com.guat.service.UserService;
import com.guat.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    //获取所有用户
    public List<User> getUserAll() {
        Connection connection = DBUtil.getConnection();
        String sql = "SELECT * from `user` ";
        List<User> list = new ArrayList<>();
        try {
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt(1));
                user.setUsername(rs.getString(2));
                user.setPassword(rs.getString(3));
                user.setType(rs.getString(4));
                list.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    //用户登录
    public User login(String username) {
        Connection connection = DBUtil.getConnection();
        String sql = "SELECT * from `user` where username = '" + username + "'";
        try {
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            User user = new User();
            if (rs.next()) {
                user.setId(rs.getInt(1));
                user.setUsername(rs.getString(2));
                user.setPassword(rs.getString(3));
                user.setType(rs.getString(4));
            }
            return user;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    //用户注册
    public void addUser(String username, String password, String type) {
        Connection connection = DBUtil.getConnection();
        String sql = "insert into `user`(username,password,type) value('" + username + "','" + password + "','" + type + "')";
        try {
            Statement statement = connection.createStatement();
            //执行sql语句
            statement.execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //删除用户
    public boolean deleteUser(String id) {
        Connection connection = DBUtil.getConnection();
        String sql = "DELETE FROM `user` WHERE id = '" + id + "'";
        try {
            Statement statement = connection.createStatement();
            //执行sql语句
            statement.execute(sql);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    //先根据id获取想要修改用户的信息
    public User doUpdateUser(String id) {
        Connection connection = DBUtil.getConnection();
        String sql = "SELECT * from `user` where id = '" + id + "'";
        try {
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            User user = new User();
            if (rs.next()) {
                user.setId(rs.getInt(1));
                user.setUsername(rs.getString(2));
                user.setPassword(rs.getString(3));
                user.setType(rs.getString(4));
            }
            return user;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    //根据用户id,修改用户数据
    public void updateUser(String id,String username,String password,String type){
        Connection connection=DBUtil.getConnection();
        String sql="update `user` set username='"+username+"',password='"+password+"',type='"+type+"' where id="+id;
        try {
            Statement statement=connection.createStatement();
            statement.execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //根据前端传回的值进行模糊查询
    public List<User> searchUser(String value) {
        Connection connection = DBUtil.getConnection();
        String sql = "SELECT * FROM user WHERE username LIKE ?";
        List<User> list = new ArrayList<>();
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, "%"+value+"%");
            ResultSet rs = statement.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt(1));
                user.setUsername(rs.getString(2));
                user.setPassword(rs.getString(3));
                user.setType(rs.getString(4));
                list.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }
}
